package com.ruoyi.mqtt.config;

import org.apache.iotdb.session.pool.SessionPool;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import lombok.extern.slf4j.Slf4j;

/**
 * IoTDB配置类
 * 用于配置IoTDB连接池
 */
@Slf4j
@Configuration
public class IoTDBConfig {

    @Value("${iotdb.host:1.95.9.103}")
    private String host;

    @Value("${iotdb.port:6667}")
    private int port;

    @Value("${iotdb.username:root}")
    private String username;

    @Value("${iotdb.password:root}")
    private String password;

    @Value("${iotdb.pool.size:20}")
    private int poolSize;

    /**
     * 创建IoTDB会话池
     * 使用Builder模式构建SessionPool，支持自动重连
     */
    @Bean(destroyMethod = "close")
    public SessionPool sessionPool() {
        SessionPool pool = new SessionPool.Builder()
            .host(host)
            .port(port)
            .user(username)
            .password(password)
            .maxSize(poolSize)
            .enableCompression(true)  // 启用压缩
            .enableRedirection(true)  // 启用重定向
            .build();

        log.info("IOTDB启动成功:IP：{}, 端口:{}, 连接池大小:{}",
                host, port, poolSize);
        return pool;
    }
}
